package com.zyht.website.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;

/**
 * <b>描述：</b>	图片验证码，验证完毕清理掉session值<br/>
 * <b>作者：</b>Bob <br/>
 * <b>修改日期：</b>2017年6月27日 - 下午2:46:19<br/>
 */
@Controller
@RequestMapping("anon")
public class ImageValidateCodeController {

	@Resource
	private DefaultKaptcha captchaProducer;
	
	@RequestMapping(method=RequestMethod.GET, value="vcode")
	public void createValidationCode(HttpServletRequest request, HttpServletResponse response){
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");
		String capText = this.captchaProducer.createText();

		request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
		BufferedImage bi = this.captchaProducer.createImage(capText);
		try {
			ImageIO.write(bi, "jpg", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
